//
//  FSCDrawerController.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/23.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit

class FSCDrawerController: UIViewController {
    
//MARK:相关属性

    //动画时长
    let animationTime:TimeInterval = 0.5
    //左边视图控制器
    let leftVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FSCLeftViewController")
    //标签视图控制器
    let tabbarVC = FSCTabBarController()
    
    //纪录手指开始位置
    var startPoint:CGPoint = CGPoint()
    
    let leftViewWidth = screenWidth * 0.842
    //左边视图的最大偏移量
    let leftViewMaxOffSet:CGFloat = screenWidth/2
    
    //计算偏移距离的比例
    var scaleOffset:CGFloat {
        return leftViewMaxOffSet / (leftViewWidth + 5)
    }
    //懒加载左边视图出现后的遮盖视图
    lazy var coverView:UIButton = {
        let btn = UIButton(type: .custom)
        btn.frame = UIScreen.main.bounds
        btn.backgroundColor = #colorLiteral(red: 0.7233663201, green: 0.7233663201, blue: 0.7233663201, alpha: 1)
        btn.alpha = 0.0
        return btn
    }()

    
//MARK:视图生命周期

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        //添加拖动手势
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureChange(gesture:)))
        view.addGestureRecognizer(panGesture)
        
    }
    
    
//MARK:设置UI
    
    func setupUI() -> Void {
        
        //添加左边控制器以及视图
        addChildViewController(leftVC)
        //添加控制器作为子控制器时，必须同时添加视图作为子视图
        view.addSubview(leftVC.view)
        
        //添加标签视图控制器
        addChildViewController(tabbarVC)
        view.addSubview(tabbarVC.view)
        
        //设置左边视图的宽度
        let leftViewWidth:CGFloat = screenWidth * 0.842
        
        //设置左边视图默认大小和位置
        leftVC.view.frame = CGRect(x: 0, y: 0, width: leftViewWidth, height: screenHeight)
        //设置右边阴影
        leftVC.view.layer.shadowColor = UIColor.gray.cgColor
        leftVC.view.layer.shadowOffset = CGSize(width: 3, height: 0)
        leftVC.view.layer.shadowOpacity = 1.0
        //设置默认的形变属性
        leftVC.view.transform = CGAffineTransform(translationX: -leftViewMaxOffSet, y: 0)
        
        //赋值闭包代码块
        tabbarVC.leftBtnClock = { [weak self] in
            //print("点击了左边按钮")
            if (self?.tabbarVC.view.transform.tx)! > 0 {
                //添加遮盖视图
                self?.closeAnimation()
            }else {
                self?.openAnimation()
            }
        }
    }

    
//MARK:拖动手势的回调方法
    func panGestureChange(gesture:UIPanGestureRecognizer) -> Void {
    
        switch gesture.state {
        case .began:
            print("开始拖动")
            //获得当前第一次得触摸点
            startPoint = gesture.location(in: gesture.view)
        case .changed:
            print("拖动中")
            //获得当前手指位移量
            let offset = gesture.translation(in: gesture.view)
            //计算两个视图的偏移量
            let newOffset = tabbarVC.view.transform.tx + offset.x
            
            //判断当偏移大于最大偏移时的处理
            if newOffset >= (leftViewWidth + 5) {
                openAnimation()
                return
            }
            if newOffset <= 0 {
                closeAnimation()
                return
            }
            
            let newOffset_leftV = leftVC.view.transform.tx + offset.x * scaleOffset
            //标签视图的形变
            tabbarVC.view.transform  = CGAffineTransform(translationX: newOffset, y: 0)
            //左边视图的偏移
            leftVC.view.transform = CGAffineTransform(translationX: newOffset_leftV, y: 0)
            
            //恢复手势移动偏移量为初始值
            gesture.setTranslation(CGPoint(), in: gesture.view)
            
        case .ended:
            print("结束")
            let endPoint = gesture.location(in: gesture.view)
            
            if endPoint.x > startPoint.x {
                //右滑
                openAnimation()
            }else {
                closeAnimation()
            }
        default:
            break
        }
    }
    
//MARK:打开和关闭抽屉的方法
    
    //打开
    func openAnimation() -> Void
    {
        tabbarVC.view.addSubview(coverView)
        coverView.addTarget(self, action: #selector(closeAnimation), for: .touchUpInside)
        //移动标签控制器视图
        UIView.animate(withDuration: animationTime) { [weak self] in
            self?.tabbarVC.view.transform = CGAffineTransform(translationX: screenWidth * 0.842 + 5, y: 0)
            self?.coverView.alpha = 0.6
            //设置左边视图的动画偏移
            self?.leftVC.view.transform = CGAffineTransform.identity
        }
        
    }

    //关闭
    func closeAnimation() -> Void {
        
        UIView.animate(withDuration: animationTime, animations: {
            
            self.tabbarVC.view.transform = CGAffineTransform.identity
            self.leftVC.view.transform = CGAffineTransform(translationX: -self.leftViewMaxOffSet, y: 0)
            self.coverView.alpha = 0.0

        }) { (_) in
            //移除当前遮盖视图
            self.coverView.removeFromSuperview()
            
            }
    }
}
